<?php
namespace app\appclass\model;
use app\BaseModel;
use think\facade\Db;



class ClassCoach extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_appclass_class_coach';


    protected $append = [

        'coach_name'

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-05-14 09:36
     * @功能说明:获取名字
     */
    public function getCoachNameAttr($value,$data){

        if(!empty($data['coach_id'])){

            return lbUserName($data['coach_id']);

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-12 10:08
     * @功能说明:添加
     */
    public function adminDataAdd($data,$class_id){

        $dis = [

            'class_id' => $class_id,

            'type'     => 0
        ];

        $this->where($dis)->delete();

        if(!empty($data['coach'])){

            foreach ($data['coach'] as $k=> $v){

                $data['coach'][$k]['class_id'] = $class_id;

                $data['coach'][$k]['uniacid']  = $data['uniacid'];

            }

            $this->saveAll($data['coach']);

        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-14 15:56
     * @功能说明:根据教练获取课程
     */
    public function classCoach($dis){

        $data  = $this->alias('a')
                ->join('longbing_card_appclass_class_list b','a.class_id = b.id')
                ->join('longbing_card_appclass_class_cate c','b.cate_id = c.id')
                ->where($dis)
                ->field('b.*,a.coach_id')
                ->group('b.id')
                ->select()
                ->toArray();

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-14 15:56
     * @功能说明:根据教练获取课程
     */
    public function classCoachList($dis,$page=10){

        $data  = $this->alias('a')
            ->join('longbing_card_appclass_class_list b','a.class_id = b.id')
            ->join('longbing_card_appclass_class_cate c','b.cate_id = c.id')
            ->where($dis)
            ->field('b.*,a.coach_id')
            ->group('b.id')
            ->paginate($page)
            ->toArray();

        return $data;

    }







}